package net.wicp.tams.plugin.task;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import net.wicp.tams.commons.apiext.TarUtil;
import net.wicp.tams.commons.constant.BasePath;

/****
 * 
 * @author zhoujunhui
 *
 */
@Mojo(name = "dkconf", requiresProject = true, threadSafe = true)
public class DuckulaConf extends AbstractMojo {

	@Parameter(defaultValue = "${basedir}", required = true)
	private String basedir;

	@Parameter(required = true)
	private String[] plugs;

	@Parameter(required = true)
	private String[] serializers;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		BasePath.projectBasePath = basedir;
		getLog().info("------------------------增加接收插件---------------------");
		String pluginDirPath = String.format("%s/target/duckula/plugin", basedir);
		File pluginDir = new File(pluginDirPath);
		pluginDir.deleteOnExit();

		installPlugs(pluginDirPath, plugs);
		getLog().info("------------------------增加序列化插件---------------------");
		// 序列化插件
		String serializerDirPath = String.format("%s/target/duckula/serializer", basedir);
		File serializerDir = new File(serializerDirPath);
		serializerDir.deleteOnExit();
		installPlugs(serializerDirPath, serializers);
		getLog().info("------------------------bin/conf/logs---------------------");
		File targetBinDir = new File(String.format("%s/target/duckula/bin", basedir));
		File binDir = new File(String.format("%s/bin/", basedir));
		try {
			FileUtils.copyDirectory(binDir, targetBinDir);
		} catch (IOException e) {
			getLog().info("bin复制错误", e);
		}

		File targetConfDir = new File(String.format("%s/target/duckula/conf", basedir));
		File confDir = new File(String.format("%s/conf/", basedir));
		try {
			FileUtils.copyDirectory(confDir, targetConfDir);
		} catch (IOException e) {
			getLog().info("conf复制错误", e);
		}
		
		File dockerConfDir = new File(String.format("%s/target/duckula", basedir));
		File dockerDir = new File(String.format("%s/docker/", basedir));
		try {
			FileUtils.copyDirectory(dockerDir, dockerConfDir);
		} catch (IOException e) {
			getLog().info("dockerfile复制错误", e);
		}

		File logsDir = new File(String.format("%s/logs/", basedir));
		File targetLogsDir = new File(String.format("%s/target/duckula/logs", basedir));
		try {
			FileUtils.copyDirectory(logsDir, targetLogsDir);
		} catch (IOException e) {
			getLog().info("logsDir复制错误", e);
		}
		getLog().info("------------------------task复制---------------------");
		String taskBasePath = String.format("%s/../%s/target/", basedir, "duckula-task");
		File taskJarFile = new File(taskBasePath + "duckula-task.jar");
		File taskLibDir = new File(taskBasePath + "lib");
		File targetDir = new File(String.format("%s/target/duckula/", basedir));
		try {
			FileUtils.copyFile(taskJarFile, new File(targetDir, "duckula-task.jar"));
			FileUtils.copyDirectory(taskLibDir, new File(targetDir, "lib"));
		} catch (IOException e) {
			getLog().info("复制task项目错误", e);
		}
		getLog().info("------------------------server复制---------------------");
		try {
			File serverJarFile = new File(
					String.format("%s/../%s/target/%s.jar", basedir, "duckula-server", "duckula-server"));
			FileUtils.copyFile(serverJarFile, new File(targetDir, "duckula-server.jar"));
		} catch (IOException e) {
			getLog().info("复制server项目错误", e);
		}
		getLog().info("------------------------ops复制---------------------");
		try {
			File serverJarFile = new File(
					String.format("%s/../%s/target/%s.war", basedir, "duckula-ops", "duckula-ops"));
			FileUtils.copyFile(serverJarFile, new File(targetDir, "duckula-ops.war"));
		} catch (IOException e) {
			getLog().info("复制server项目错误", e);
		}
		getLog().info("------------------------打tar包---------------------");
		TarUtil.compress(targetDir, targetDir.getPath() + "duckula.tar");
		getLog().info("------------------------dkconf完成---------------------");
	}

	private void installPlugs(String pluginDirPath, String[] plugNames) throws MojoFailureException {
		for (String plugName : plugNames) {
			try {
				File srcFile = new File(String.format("%s/../%s/target/%s.jar", basedir, plugName, plugName));
				File targetFile = new File(String.format("%s/%s/%s.jar", pluginDirPath, plugName, plugName));
				FileUtils.copyFile(srcFile, targetFile);
				File libDir = new File(String.format("%s/../%s/target/lib", basedir, plugName));
				File targetLib = new File(String.format("%s/%s", pluginDirPath, plugName));
				FileUtils.copyDirectoryToDirectory(libDir, targetLib);
			} catch (IOException e) {
				throw new MojoFailureException("复制插件有问题", e);
			}
		}
	}

}
